package me.xplabs.ui.baseui 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import me.xplabs.Globals;
	import me.xplabs.ui.BaseSprite;
	
	/**
	 * ...
	 * @author xiaohan
	 */
	public class Button extends BaseSprite 
	{
		private var _normal					:BitmapData;
		private var _light					:BitmapData;
		private var _press					:BitmapData;
		private var _bitmap					:Bitmap;
		public var callback					:Function;
		public var overCall					:Function;
		public var outCall					:Function;
		public var downCall					:Function;
		public var label					:Label;
		public var showedHand				:Boolean
		public var typeId					:int;
		public var tabState					:Boolean;
		public var tabClick					:Boolean;
		public function Button() 
		{
			
		}
		public function show(ptext : String, up : BitmapData, down : BitmapData, high : BitmapData):void
		{
			_normal = up;
			_light = high;
			_press = down;
			_bitmap = new Bitmap(_normal);
			addChild(_bitmap);
			
			label = Globals.ui.label(ptext);
			label.mouseEnabled = false;
			label.mouseChildren = false;
			label.x = (_bitmap.width - label.width) * .5;
			label.y = (_bitmap.height - label.height) * .5;
			addChild(label);
			buttonMode = true;
			
			addEventListener(MouseEvent.CLICK, clickHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
		}
		
		public function setTabState(boolean:Boolean = true):void 
		{
			_bitmap.bitmapData = _press;
			tabClick = true;
		}
		
		public function defaultStyle():void 
		{
			tabClick = false;
			_bitmap.bitmapData = _normal;
		}
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			if (tabState) return;
			_bitmap.bitmapData = _light;
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			_bitmap.bitmapData = _press;
			if (downCall != null) downCall(e);
		}
		
		private function rollOverHandler(e:MouseEvent):void 
		{
			if (!tabClick) _bitmap.bitmapData = _light;
			if (overCall != null) overCall(e);
		}
		
		private function rollOutHandler(e:MouseEvent):void 
		{
			if (!tabClick) _bitmap.bitmapData = _normal;
			if (outCall != null) outCall(e);
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			if (!tabClick) if (callback != null) callback(e);
		}
		public function set text(value:String):void
		{
			label.text = value;
		}
		public function get text():String
		{
			return label.text;
		}
	}
}